<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商家名称" prop="shopsName">
        <el-input
            v-model="queryParams.shopsName"
            placeholder="请输入商家名称"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类别" prop="category">
        <el-input
            v-model="queryParams.category"
            placeholder="请输入类别"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:scooper:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:scooper:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:scooper:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:scooper:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scooperList" @selection-change="handleSelectionChange">
      <el-table-column label="商家名称" align="center" prop="shopsName"/>
      <el-table-column label="类别" align="center" prop="category"/>
      <el-table-column label="详细地址" align="center" prop="address"/>
      <el-table-column label="微信号" align="center" prop="weixin"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['scooper:query']">
            详细
          </el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['scooper:edit']">
            修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['scooper:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改捞客门店信息对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="scooperRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="商家名称" prop="shopsName">
          <el-input v-model="form.shopsName" placeholder="请输入商家名称"/>
        </el-form-item>
        <el-form-item label="类别" prop="category">
          <el-input v-model="form.category" placeholder="请输入类别"/>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址"/>
        </el-form-item>
        <el-form-item label="省" prop="province">
          <el-input v-model="form.province" placeholder="请输入省"/>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="form.city" placeholder="请输入市"/>
        </el-form-item>
        <el-form-item label="县" prop="county">
          <el-input v-model="form.county" placeholder="请输入县"/>
        </el-form-item>
        <el-form-item label="微信号" prop="weixin">
          <el-input v-model="form.weixin" placeholder="请输入微信号"/>
        </el-form-item>
        <el-form-item label="门店log" prop="shopsLogo">
          <el-input v-model="form.shopsLogo" placeholder="请输入门店log"/>
        </el-form-item>
        <el-form-item label="门店宣传图片" prop="advertise">
          <el-input v-model="form.advertise" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="用户ID" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入用户ID"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 商铺详细 -->
    <el-dialog title="商铺详细" v-model="detailOpen" width="800px" append-to-body>
      <el-form :model="form" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="商家名称：">{{ form.shopsName }}</el-form-item>
            <el-form-item label="详细地址：">{{ form.address }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="类别：">{{ form.category }}</el-form-item>
            <el-form-item label="省市区：">{{ form.province }}-{{ form.city }}-{{ form.county }}</el-form-item>
          </el-col>
          <el-divider/>
          <el-col :span="24">
            <block v-if="form.weixins.length > 0">
              <el-form-item label="微信号：" v-for="item in form.weixins">{{ item }}</el-form-item>
            </block>
            <block v-else>
              <el-form-item label="微信号："></el-form-item>
            </block>
          </el-col>
          <el-divider/>
          <el-col :span="24">
            <el-form-item label="门店log：">{{form.shops_logo ? imgUrl(form.shops_logo):''}}</el-form-item>
          </el-col>
          <el-divider/>
          <el-col :span="24">
            <block v-if="form.advertises.length > 0">
              <el-form-item label="门店宣传图片：" v-for="item in form.advertises">{{imgUrl(item)}}</el-form-item>
            </block>
            <el-col :span="24">
              <el-form-item label="门店宣传图片："></el-form-item>
            </el-col>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailOpen = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Scooper">
import {listScooper, getScooper, delScooper, addScooper, updateScooper} from "@/api/scooper/scooper";

const {proxy} = getCurrentInstance();

const scooperList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const detailOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    shopsName: null,
    category: null,
  },
  rules: {}
});

const {queryParams, form, rules} = toRefs(data);

/** 查询捞客门店信息列表 */
function getList() {
  loading.value = true;
  listScooper(queryParams.value).then(response => {
    scooperList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    shopsName: null,
    category: null,
    address: null,
    province: null,
    city: null,
    county: null,
    weixin: null,
    shopsLogo: null,
    advertise: null,
    uid: null,
    remark: null,
    sort: null,
    status: null,
    isDel: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("scooperRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加捞客门店信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getScooper(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改捞客门店信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["scooperRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateScooper(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addScooper(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除捞客门店信息编号为"' + _ids + '"的数据项？').then(function () {
    return delScooper(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/scooper/export', {
    ...queryParams.value
  }, `scooper_${new Date().getTime()}.xlsx`)
}

/** 详细按钮操作 */
async function handleView(row) {
  await getScooper(row.id).then(response => {
    form.value = response.data;
  });
  detailOpen.value = true;
}
/** 拼接图片地址*/
function imgUrl(url){
  return import.meta.env.VITE_APP_BASE_API + "/" + url
}

getList();
</script>
